﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace Transport
{
    /// <summary>
    /// Summary description for Service
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        public string HelloWorld()
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
                return "Hello World";
            else
                return "You are not authorized user!";
        }

        [WebMethod (EnableSession=true)]
        public bool DangNhap(string username, string password)
        {
            bool flag = false;

            NguoiDungControl userctrl = new NguoiDungControl();
            nguoi_dung user = userctrl.findByUsername(username);

            if (user != null)
                if (user.password == password)
                    if (user.loai_nguoi_dung == "PARTNER")
                        flag = true;

            if (flag)
            {
                Session.Add("IsLogin", true);
                Session.Add("Username", username);
                return true;
            }
            else
            {
                FormsAuthentication.SignOut();
                return false;
            }

        }

        [WebMethod(EnableSession = true)]
        public List<BangGiaTrongNgay> BangGiaTrongNgay(string dichvu)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                BangGiaTrongNgayControl bangGia = new BangGiaTrongNgayControl();

                return bangGia.BangGiaTrongNgay(dichvu);
            }
            else
                return null;
        }

        [WebMethod(EnableSession = true)]
        public List<BangGiaTheoMien> GiaTheoMien(string mien)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                BangGiaTheoMienControl bangGia = new BangGiaTheoMienControl();

                return bangGia.GiaTheoMien(mien);
            }
            else
                return null;
        }

        [WebMethod(EnableSession = true)]
        public List<Xe> TimXeTheoCho(int SoCho)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                XeControl bangGia = new XeControl();

                return bangGia.TimXeTheoSoCho(SoCho);
            }
            else
                return null;
        }

        [WebMethod(EnableSession = true)]
        public List<Xe> TimXeTheoHieu(string Hieu)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                XeControl bangxe = new XeControl();

                return bangxe.TimXeTheoHieu(Hieu);
            }
            else
                return null;
        }

        [WebMethod(EnableSession = true)]
        public List<Xe> TimXeTheoDichVu(string DichVu)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                XeControl bangxe = new XeControl();

                return bangxe.TimXeTheoDichVu(DichVu);
            }
            else
                return null;
        }

        [WebMethod(EnableSession = true)]
        public string DatXeTheoTour(string malotrinh, int loaixe, int soluongxe,
            DateTime ngaydi, DateTime ngayve, DateTime giodon, string diadiemdon)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                ChiTietDatXeControl chitiet = new ChiTietDatXeControl();
                chi_tiet_dat_xe info = new chi_tiet_dat_xe();

                info.ten_dang_nhap = (string)Session["Username"];
                info.ma_lo_trinh = malotrinh;
                info.so_cho = loaixe;
                info.so_luong_xe = soluongxe;
                info.tu_ngay = ngaydi;
                info.den_ngay = ngayve;
                info.gio_don = giodon.Subtract(giodon.Date);
                info.dia_diem_don = diadiemdon;

                return chitiet.insertTheoTour(info);
            }
            else
                return null;
        }

        [WebMethod (EnableSession=true)]
        public string DatXeTheoYeuCau(string phuongthuclai, string loaidichvu, int loaixe, int soluongxe,
            DateTime ngaydi, DateTime ngayve, DateTime giodon, string diadiemdon, string mien, string ghichu)
        {
            if (Session["IsLogin"] != null && (bool)Session["IsLogin"])
            {
                ChiTietDatXeControl chitiet = new ChiTietDatXeControl();
                DichVuControl dv = new DichVuControl();
                MienControl rg = new MienControl();
                chi_tiet_dat_xe info = new chi_tiet_dat_xe();

                info.ten_dang_nhap = (string)Session["Username"];

                if (phuongthuclai == "Tự lái")
                    info.phuong_thuc_lai = "0";
                else
                    info.phuong_thuc_lai = "1";

                string dvid = dv.getServiceId(loaidichvu);

                if (dvid != "")
                    info.ma_dich_vu = dvid;
                else
                    info.ma_dich_vu = "0";

                info.so_cho = loaixe;
                info.so_luong_xe = soluongxe;
                info.tu_ngay = ngaydi;
                info.den_ngay = ngayve;
                info.gio_don = giodon.Subtract(giodon.Date);
                info.dia_diem_don = diadiemdon;

                string idmien = rg.getID(mien);
                if (idmien != "")
                    info.ma_mien = idmien;
                else
                    info.ma_mien = "0";

                info.ghi_chu = ghichu;

                return chitiet.insertTheoYeuCau(info);
            }
            else
                return null;
        }

    }
}
